package com.masterclient.app.businessmodule.vipmodule.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.arjinmc.recyclerviewdecoration.RecyclerViewItemDecoration;
import com.eoner.baselib.fragment.FCBaseFragment;
import com.eoner.baselib.widget.navgation.FCNavigationBar;
import com.eoner.baselibrary.utils.DensityUtil;
import com.eoner.uikit.header.MyRefreshHead;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.homemodule.view.footer.HomeClassicFooter;
import com.masterclient.app.businessmodule.vipmodule.adapter.SaleOrderAdapter;
import com.masterclient.app.businessmodule.vipmodule.adapter.ServiceOrderAdapter;
import com.masterclient.app.businessmodule.vipmodule.bean.Order;
import com.masterclient.app.businessmodule.vipmodule.bean.OrderBean;
import com.masterclient.app.businessmodule.vipmodule.bean.ServiceOrder;
import com.masterclient.app.businessmodule.vipmodule.bean.ServiceOrderBean;
import com.masterclient.app.businessmodule.vipmodule.persenter.SaleOrderListContract;
import com.masterclient.app.businessmodule.vipmodule.persenter.SaleOrderListPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import java.util.List;

import butterknife.BindView;

import static com.eoner.uikit.header.MyRefreshHead.LOTTIE;

/**
 * 订单
 */
public class OrderFragment extends FCBaseFragment<SaleOrderListPresenter>
        implements SaleOrderListContract.View {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.smartRefresh)
    SmartRefreshLayout smartRefresh;
    @BindView(R.id.status_iv)
    ImageView statusIv;
    @BindView(R.id.status_tv)
    TextView statusTv;
    @BindView(R.id.empty_view)
    LinearLayout emptyView;
    private String params;
    private int type;
    private SaleOrderAdapter saleOrderAdapter;
    private ServiceOrderAdapter serviceOrderAdapter;
    private int page = 1;
    private int pageSize = 15;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (null != bundle) {
            type = bundle.getInt("type");
            params = bundle.getString("params");
        }
    }

    public static OrderFragment newInstance(int type, String params) {
        OrderFragment fragment = new OrderFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        bundle.putString("params", params);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int requireGetLayoutId() {
        return R.layout.fragment_list;
    }

    @Override
    protected void requireSetCustomNavigationBar(FCNavigationBar navigationBar) {
        navigationBar.setVisibility(View.GONE);
    }

    @Override
    protected void requireInitUIAndData(Bundle savedInstanceState) {
        initRecycler();
        requestData(true);
    }


    /**
     * 初始化recyclerview
     */
    private void initRecycler() {
        MyRefreshHead myRefreshHead = new MyRefreshHead(getContext(), 1, LOTTIE);
        myRefreshHead.changeBackColor(Color.parseColor("#212121"));
        myRefreshHead.changeTextColor(Color.parseColor("#FFFFFF"));
        smartRefresh.setRefreshHeader(myRefreshHead);
        smartRefresh.setRefreshFooter(new HomeClassicFooter(getContext()));
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.addItemDecoration(new RecyclerViewItemDecoration.Builder(getContext())
                .thickness(DensityUtil.dip2px(8))
                .color(getResources().getColor(R.color.color_EEEEEE))
                .firstLineVisible(true)
                .lastLineVisible(true)
                .create());
        setLoadMoreAndRefresh();
        if (0 == type) {// 设置零售订单adapter
            saleOrderAdapter = new SaleOrderAdapter(getContext());
            recyclerView.setAdapter(saleOrderAdapter);
        } else {// 设置售后订单adapter
            serviceOrderAdapter = new ServiceOrderAdapter(getContext());
            recyclerView.setAdapter(serviceOrderAdapter);
        }

    }

    /**
     * 设置加载更多和刷新
     */
    private void setLoadMoreAndRefresh() {
        smartRefresh.setOnRefreshListener(refreshLayout -> {
            refresh();
        });

        smartRefresh.setOnLoadMoreListener(refreshLayout -> {
            requestData(false);
        });
    }

    public void refresh(){
        page = 1;
        smartRefresh.resetNoMoreData();
        requestData(false);
    }

    /**
     * 请求数据
     * @param showLoading
     */
    private void requestData(boolean showLoading){
        if (0 == type) {// 设置零售订单请求
            if (params.equals("-1")) {// 已移交订单
                mPresenter.getNotOrderList(showLoading, page, pageSize);
            } else {
                mPresenter.getOrderList(showLoading, params, page, pageSize);
            }
        }else {// 售后订单
            mPresenter.getRefundList(showLoading,params,page,pageSize);
        }
    }

    private void refreshEmptyStateAndMsg(int imgId, String msg) {
        smartRefresh.finishLoadMore();
        smartRefresh.finishRefresh();
        if (saleOrderAdapter != null && saleOrderAdapter.getItemCount() == 0) {
            emptyView.setVisibility(View.VISIBLE);
            statusIv.setImageResource(imgId);
            statusTv.setText(msg);
        }
        if (serviceOrderAdapter != null && serviceOrderAdapter.getItemCount() == 0) {
            emptyView.setVisibility(View.VISIBLE);
            statusIv.setImageResource(imgId);
            statusTv.setText(msg);
        }
    }

    @Override
    public void getOrderList(OrderBean orderBean) {
        if(null != orderBean){
            List<Order> orders = orderBean.getList();
            setData(orders);
            if (null != orders && orders.size() > 0) {
                page = page + 1;
            }else {
                smartRefresh.setNoMoreData(true);
            }
        }else {
            smartRefresh.setNoMoreData(true);
        }
        refreshEmptyStateAndMsg(R.drawable.iconempty_list, "一贫如洗的订单");
    }

    /**
     * 售后订单数据返回
     * @param order
     */
    @Override
    public void getServiceOrderList(ServiceOrderBean order) {
        if(null != order){
            List<ServiceOrder> orders = order.getRefund_list();
            setServiceData(orders);
            if (null != orders && orders.size() > 0) {
                page = page + 1;
            }else {
                smartRefresh.setNoMoreData(true);
            }
        }else {
            smartRefresh.setNoMoreData(true);
        }
        refreshEmptyStateAndMsg(R.drawable.iconempty_list, "一贫如洗的订单");
    }

    @Override
    public void error(String error) {
        refreshEmptyStateAndMsg(R.drawable.iconempty_list, error);
    }

    /**
     * 设置adapter数据源
     * @param orders
     */
    private void setData(List<Order> orders){
        if(page == 1){
            if(saleOrderAdapter != null)
                saleOrderAdapter.setNewData(orders);
        }else {
            if(saleOrderAdapter != null)
                saleOrderAdapter.setOrderList(orders);
        }
    }

    /**
     * 设置售后订单adapter数据源
     * @param orders
     */
    private void setServiceData(List<ServiceOrder> orders){
        if(page == 1){
            if(serviceOrderAdapter != null)
                serviceOrderAdapter.setNewData(orders);
        }else {
            if(serviceOrderAdapter != null)
                serviceOrderAdapter.setOrderList(orders);
        }
    }
}
